import 'package:shared_preferences/shared_preferences.dart';
import '../models/user_model.dart';

/// 本地存储服务类
/// 负责用户信息、设置等数据的本地存储
class StorageService {
  static SharedPreferences? _prefs;
  
  // 用户相关键名
  static const String _keyUserToken = 'user_token';
  static const String _keyUserInfo = 'user_info';
  static const String _keyIsLoggedIn = 'is_logged_in';
  static const String _keyIsFirstLogin = 'is_first_login';
  static const String _keyUserType = 'user_type';
  static const String _keyLastLoginTime = 'last_login_time';
  
  // 设置相关键名
  static const String _keyAutoLogin = 'auto_login';
  static const String _keyRememberPassword = 'remember_password';
  static const String _keyNotificationEnabled = 'notification_enabled';
  static const String _keyThemeMode = 'theme_mode';
  
  /// 初始化存储服务
  static Future<void> init() async {
    _prefs ??= await SharedPreferences.getInstance();
  }
  
  /// 获取SharedPreferences实例
  static Future<SharedPreferences> get _preferences async {
    _prefs ??= await SharedPreferences.getInstance();
    return _prefs!;
  }
  
  // ==================== 用户认证相关 ====================
  
  /// 保存用户Token
  static Future<bool> saveUserToken(String token) async {
    final prefs = await _preferences;
    return await prefs.setString(_keyUserToken, token);
  }
  
  /// 获取用户Token
  static Future<String?> getUserToken() async {
    final prefs = await _preferences;
    return prefs.getString(_keyUserToken);
  }
  
  /// 保存用户信息
  static Future<bool> saveUserInfo(UserModel user) async {
    final prefs = await _preferences;
    return await prefs.setString(_keyUserInfo, user.toJson().toString());
  }
  
  /// 获取用户信息
  static Future<UserModel?> getUserInfo() async {
    final prefs = await _preferences;
    final userJson = prefs.getString(_keyUserInfo);
    if (userJson != null) {
      try {
        // 这里需要解析JSON字符串，简化处理
        return null; // 实际实现中需要解析JSON
      } catch (e) {
        return null;
      }
    }
    return null;
  }
  
  /// 设置登录状态
  static Future<bool> setLoggedIn(bool isLoggedIn) async {
    final prefs = await _preferences;
    return await prefs.setBool(_keyIsLoggedIn, isLoggedIn);
  }
  
  /// 获取登录状态
  static Future<bool> isLoggedIn() async {
    final prefs = await _preferences;
    return prefs.getBool(_keyIsLoggedIn) ?? false;
  }
  
  /// 设置是否首次登录
  static Future<bool> setIsFirstLogin(bool isFirstLogin) async {
    final prefs = await _preferences;
    return await prefs.setBool(_keyIsFirstLogin, isFirstLogin);
  }
  
  /// 获取是否首次登录
  static Future<bool> isFirstLogin() async {
    final prefs = await _preferences;
    return prefs.getBool(_keyIsFirstLogin) ?? true;
  }
  
  /// 设置用户类型
  static Future<bool> setUserType(int userType) async {
    final prefs = await _preferences;
    return await prefs.setInt(_keyUserType, userType);
  }
  
  /// 获取用户类型
  static Future<int> getUserType() async {
    final prefs = await _preferences;
    return prefs.getInt(_keyUserType) ?? 2; // 默认为店主
  }
  
  /// 保存最后登录时间
  static Future<bool> saveLastLoginTime() async {
    final prefs = await _preferences;
    return await prefs.setString(_keyLastLoginTime, DateTime.now().toIso8601String());
  }
  
  /// 获取最后登录时间
  static Future<DateTime?> getLastLoginTime() async {
    final prefs = await _preferences;
    final timeString = prefs.getString(_keyLastLoginTime);
    if (timeString != null) {
      try {
        return DateTime.parse(timeString);
      } catch (e) {
        return null;
      }
    }
    return null;
  }
  
  // ==================== 应用设置相关 ====================
  
  /// 设置自动登录
  static Future<bool> setAutoLogin(bool autoLogin) async {
    final prefs = await _preferences;
    return await prefs.setBool(_keyAutoLogin, autoLogin);
  }
  
  /// 获取自动登录设置
  static Future<bool> getAutoLogin() async {
    final prefs = await _preferences;
    return prefs.getBool(_keyAutoLogin) ?? false;
  }
  
  /// 设置记住密码
  static Future<bool> setRememberPassword(bool rememberPassword) async {
    final prefs = await _preferences;
    return await prefs.setBool(_keyRememberPassword, rememberPassword);
  }
  
  /// 获取记住密码设置
  static Future<bool> getRememberPassword() async {
    final prefs = await _preferences;
    return prefs.getBool(_keyRememberPassword) ?? false;
  }
  
  /// 设置通知开关
  static Future<bool> setNotificationEnabled(bool enabled) async {
    final prefs = await _preferences;
    return await prefs.setBool(_keyNotificationEnabled, enabled);
  }
  
  /// 获取通知开关
  static Future<bool> getNotificationEnabled() async {
    final prefs = await _preferences;
    return prefs.getBool(_keyNotificationEnabled) ?? true;
  }
  
  /// 设置主题模式
  static Future<bool> setThemeMode(String themeMode) async {
    final prefs = await _preferences;
    return await prefs.setString(_keyThemeMode, themeMode);
  }
  
  /// 获取主题模式
  static Future<String> getThemeMode() async {
    final prefs = await _preferences;
    return prefs.getString(_keyThemeMode) ?? 'system';
  }
  
  // ==================== 通用方法 ====================
  
  /// 保存字符串
  static Future<bool> saveString(String key, String value) async {
    final prefs = await _preferences;
    return await prefs.setString(key, value);
  }
  
  /// 获取字符串
  static Future<String?> getString(String key) async {
    final prefs = await _preferences;
    return prefs.getString(key);
  }
  
  /// 保存整数
  static Future<bool> saveInt(String key, int value) async {
    final prefs = await _preferences;
    return await prefs.setInt(key, value);
  }
  
  /// 获取整数
  static Future<int?> getInt(String key) async {
    final prefs = await _preferences;
    return prefs.getInt(key);
  }
  
  /// 保存布尔值
  static Future<bool> saveBool(String key, bool value) async {
    final prefs = await _preferences;
    return await prefs.setBool(key, value);
  }
  
  /// 获取布尔值
  static Future<bool?> getBool(String key) async {
    final prefs = await _preferences;
    return prefs.getBool(key);
  }
  
  /// 删除指定键
  static Future<bool> remove(String key) async {
    final prefs = await _preferences;
    return await prefs.remove(key);
  }
  
  /// 清除所有数据
  static Future<bool> clear() async {
    final prefs = await _preferences;
    return await prefs.clear();
  }
  
  /// 清除用户相关数据
  static Future<void> clearUserData() async {
    final prefs = await _preferences;
    await prefs.remove(_keyUserToken);
    await prefs.remove(_keyUserInfo);
    await prefs.remove(_keyIsLoggedIn);
    await prefs.remove(_keyUserType);
    await prefs.remove(_keyLastLoginTime);
  }
}
